<template>
<div class='g-content-wrapper'>
  <header-top>大单资金</header-top>
  <div class="g-content">
    <state></state>
    <tab-bar :tabBar="menuTabBar" :on-selected="onBarSelected"></tab-bar>
    <div v-if = "msgBar == 0">
      <chart-connect :chart-data = "chartConnectData"></chart-connect>
      <div class="interval"></div>
      <detail-list :list-type="listType" :list-data="listData" :head-content = 'headContent' :updateTime="listUpdateTime">大单资金流入标的</detail-list>
    </div>
    <chart-overlap v-else :chart-data = "chartOverlapData"></chart-overlap>
    <div class="interval"></div>
    <div class='comment'>
    <h1 class='comment-title'>大单资金</h1>
      <p>以沪深两市所有股票为统计对象，根据股价、占流通盘比例、成交数量来确定大单的范围，统计所有大单的资金流入流出情况。<br/>大单资金能够反应大户的动向，在一定程度上推动股价涨跌。</p>
    </div>
  </div>
</div>
</template>

<script>
import headerTop from '@/components/common/HeaderTop'
import state from '@/components/common/State'
import tabBar from '@/components/common/TabBar'
import chartConnect from '@/components/common/ChartConnect'
import chartOverlap from '@/components/common/ChartOverlap'
import detailList from '@/components/common/DetailList'
import remoteDAO from '@/common//remoteDAO'
import constant from '@/common/constant'
import tools from 'libs/common/tools'
import { transform1013 } from '@/common/common'
import { Loop } from '@/common/polling'

const timeFromMinuts = m => {
  let h = m / 60;
  h = h > 10 ? h : '0' + h;
  let min = m % 60;
  min = min > 10 ? min : '0' + min;
  return `${h}:${min}`
};
// 当日大单联动图表数据
const chartConnectData = data => {
  let xTime = [],
    netNumber = [],
    mIn = [],
    mOut = [],
    mNetNumber = [];
  data.TableInfo.forEach(item => {
    xTime.push(timeFormMinuts(item.minute));
    netNumber.push(item.ljjlr);
    mIn.push(item.bbmoney);
    mOut.push(item.sbmoney);
    mNetNumber.push(item.ddjlr)
  });
  return {
    xTime: xTime,
    netNumber: netNumber,
    minute: {
      in: mIn,
      out: mOut,
      mNetNumber: mNetNumber
    }
  }
}

// 近一月、近三月图表数据
// const chartOverlapData = data => {
// let xTime = [],
//   netNumber = [],
//   mIn = [],
//   mOut = [],
//   mNetNumber = [];
// data.TableInfo.forEach(item => {
//   xTime.push(timeFormMinuts(item.minute));
//   netNumber.push(item.ljjlr);
//   mIn.push(item.bbmoney);
//   mOut.push(item.sbmoney);
//   mNetNumber.push(item.ddjlr)
// });
// return {
//   xTime: xTime,
//   netNumber: netNumber,
//   minute: {
//     in: mIn,
//     out: mOut,
//     mNetNumber: mNetNumber
//   }
// }
// }
export default {
  name: 'large-order',
  data () {
    return {
      menuTabBar: [{name: '今日'}, {name: '近一月'}, {name: '近三月'}],
      msgBar: 0,
      listUpdateTime: '',
      listType: 'c',
      listData: [],
      headContent: ['流入标的', '现价', '净流入'],
      chartConnectData: {},
      chartOverlapData: null,
      topDesc: '',
      chartData: [],
      chartOverlapData: null,
      topDesc: ''
    }
  },
  components: {
    headerTop,
    state,
    tabBar,
    chartConnect,
    chartOverlap,
    detailList
  },
  methods: {
    onBarSelected (index) {
      if (this.msgBar == index) {
        return;
      }
      this.msgBar = index;
      this.$store.dispatch('showLoading');
      Loop.getInstance().register(this.fetchData);
      this.fetchData();
    },
    fetchData () {
      this.msgBar == 0 ? this.fetchCurrentData() : this.fetchLastData();
    },
//  获取当日大单资金数据
//  获取当日大单资金流入标的数据
    async fetchCurrentData () {
      try {
        this.$store.dispatch('showLoading');
<<<<<<< .mine
        console.log(this.msgBar)
        const res = await Promise.all([
          remoteDAO.invoke(
            constant.InterfaceEnum.GET_C_M_FUND,
            { type: this.msgBar }
          )
          // remoteDAO.invoke(
          //   constant.InterfaceEnum.GET_C_L_O_FUND,
          //   { type: this.msgBar }
          // ),
          // remoteDAO.invoke(
          //   constant.InterfaceEnum.GET_LAST_INDEX,
          //   { type: this.msgBar }
          // )
        ])
        console.log('res', res)
        this.chartConnectData = {
          xTime: res[1].xTime,
          netNumber: res[1].netNumber,
          minute: {
            in: res[1].minute.in,
            out: res[1].minute.out.map(v => -v),
            mNetNumber: res[1].minute.in.map((v, i) => v - res[1].minute.out[i])
          },
          lastIndex: res[2],
          indexData: {
            index: res[0],
            closing: res[2]
          }
        }
        this.listData = res[1].stocks
        this.listUpdateTime = `${tools.DateFormat(new Date(), 'yyyy-MM-dd')}（今日）`
        this.$store.dispatch('hideLoading');
||||||| .r40718
        const res = await Promise.all([
          remoteDAO.invoke(
            constant.InterfaceEnum.GET_INDEX,
            { type: this.msgBar }
          ),
          remoteDAO.invoke(
            constant.InterfaceEnum.GET_C_L_O_FUND,
            { type: this.msgBar }
          ),
          remoteDAO.invoke(
            constant.InterfaceEnum.GET_LAST_INDEX,
            { type: this.msgBar }
          )
        ])
        this.chartConnectData = {
          xTime: res[1].xTime,
          netNumber: res[1].netNumber,
          minute: {
            in: res[1].minute.in,
            out: res[1].minute.out.map(v => -v),
            mNetNumber: res[1].minute.in.map((v, i) => v - res[1].minute.out[i])
          },
          lastIndex: res[2],
          indexData: {
            index: res[0],
            closing: res[2]
          }
        }
        this.listData = res[1].stocks
        this.listUpdateTime = `${tools.DateFormat(new Date(), 'yyyy-MM-dd')}（今日）`
        this.$store.dispatch('hideLoading');
=======
        const [connectChartData, index, intoUnderlying] = await Promise.all([
          remoteDAO.invoke(constant.InterfaceEnum.GET_C_M_FUND, { type: 0 }),
          remoteDAO.invoke(constant.InterfaceEnum.GER_INDEX, {type: 0}),
          remoteDAO.invoke(constant.InterfaceEnum.GET_C_M_STOCK, {type: 2})
        ]);
        this.chartData = {
          ...chartConnectData(connectChartData),
          indexData: index
        };
        this.listUpdateTime = intoUnderlying.date.slice(-4).replace(/(^.{2})/, '$1' + ':');
        intoUnderlying.TableInfo.forEach((item, index) => {
          if (index >= 10) return;
          this.listData.push({
            strName: item.stockname,
            strCode: item.stock,
            netFlowIn: item.jlr,
            nAmount: item.zd,
            nRate: item.zdf,
            nPrice: item.Match
          })
        })
>>>>>>> .r40953
      } catch (err) {
        this.$store.dispatch('showError');
      }
    },
<<<<<<< .mine
    async fetchLastData () {
      try {
        this.$store.dispatch('showLoading');
        const res = await Promise.all([
          remoteDAO.invoke(
            constant.InterfaceEnum.GET_LST_L_O_FUND,
            { type: this.msgBar }
          ),
          remoteDAO.invoke(
            constant.InterfaceEnum.GET_INDEX,
            { type: this.msgBar }
          ),
          remoteDAO.invoke(
            constant.InterfaceEnum.GET_LAST_INDEX,
            { type: this.msgBar }
          )])
        this.chartOverlapData = {
          ...res[0],
          indexData: res[0].shIndex
        }
        this.$store.dispatch('hideLoading');
      } catch (err) {
        console.log(`ERROR: ${err}`)
      }
    },
||||||| .r40718
    async fetchLastData () {
      try {
        this.$store.dispatch('showLoading');
        const res = await Promise.all([
          remoteDAO.invoke(
            constant.InterfaceEnum.GET_LST_L_O_FUND,
            { type: this.msgBar }
          ),
          remoteDAO.invoke(
            constant.InterfaceEnum.GET_INDEX,
            { type: this.msgBar }
          ),
          remoteDAO.invoke(
            constant.InterfaceEnum.GET_LAST_INDEX,
            { type: this.msgBar }
          )])
        this.chartOverlapData = {
          ...res[0],
          indexData: res[0].shIndex
        }
        this.$store.dispatch('hideLoading');
      } catch (err) {
        console.log(`ERROR: ${err}`)
      }
    },
=======
//  获取近一月近三月大单资金数据
//  async fetchLastData () {
//    try {
//      this.$store.dispatch('showLoading');
//      const [overlapChartData, index] = await Promise.all([
//        remoteDAO.invoke(constant.InterfaceEnum.GET_DAYS_FUND, { type: 0 }, {pType: this.msgBar ? 30 : 90}),
//        remoteDAO.invoke(constant.InterfaceEnum.GER_INDEX, { type: 0 })
//      ])
//      const data = overlapChartData(chart);
//      data.indexData = index.data;
//      this.chartConnectData = data;
//    } catch (err) {
//      this.$store.dispatch('showError');
//    }
//  },
//  获取state数据
>>>>>>> .r40953
    async fetchTopDesc(type = 5) {
      try {
        const res = await remoteDAO.invoke(constant.InterfaceEnum.GET_TOP_DESC, {type})
        this.topDesc = res
      } catch (err) {
        console.log(`ERROR: ${err}`)
      }
    }
  },
  created() {
    this.fetchTopDesc();
  },
  mounted () {
    Loop.getInstance().register(this.fetchData, this.fetchStkData);
  }
}
</script>

<style lang="less" scoped>
  @import '../../assets/css/common.less';
  @import '../../assets/css/cssReset';
</style>
